package com.topcheer.common.auth.interceptor;

import com.topcheer.common.auth.annotation.IngoreAuth;
import com.topcheer.common.auth.context.BaseContextHandler;
import com.topcheer.common.auth.feign.RbacService;
import com.topcheer.common.base.util.SimpleAntPathMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xutao
 * @Package com.example.baseline.rbac.interceptor
 * @Description: 权限拦截器
 * @date 2020/3/31 20:19
 */
public class UserAuthInterceptor extends HandlerInterceptorAdapter {

    public static final String[] AUTH_WHITELIST = {
        // -- swagger ui
        "/v2/api-docs",
        "/swagger-resources",
        "/swagger-resources/**",
        "/configuration/ui",
        "/configuration/security",
        "/swagger-ui.html",
        "/webjars/**",
        "/error",
        "/client/**",
        //            "/role/permission/**",
        "/user/baseUser/**",
        "/jwt/**"
        // other public endpoints of your API may be appended to this array
    };

    @Autowired
    SimpleAntPathMatcher simpleAntPathMatcher;
    @Autowired
    RbacService rbacService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        String requestURI = request.getRequestURI();
        if (simpleAntPathMatcher.match(AUTH_WHITELIST, requestURI, "")) {
            return super.preHandle(request, response, handler);
        }
        //language=JSON
        String str = "";

        HandlerMethod handlerMethod = (HandlerMethod)handler;
        IngoreAuth annotation = handlerMethod.getBeanType().getAnnotation(IngoreAuth.class);
        if (annotation == null) {
            annotation = handlerMethod.getMethodAnnotation(IngoreAuth.class);
        }
        if (annotation != null) {
            return super.preHandle(request, response, handler);
        }
        return true;
        // TODO 此处不合理 可以挪到gateway验证  feign验证使用service auth验证
        /*String patternUrl = WebUtil.getPatternUrl(handlerMethod);
        String userID = BaseContextHandler.getUsername();
        BaseUser userInfo = rbacService.permission(userID).getData();
        List<Operation> operationList = userInfo.getOperationList();
        for(Operation operation:operationList){
            if(patternUrl.equals(operation.getOperationUrl())){
                return super.preHandle(request, response, handler);
            }
        }
        WebUtil.renderMsg(response,HttpStatus.FORBIDDEN.value(),"this is no auth to access please contact the admin ");
        return false;*/
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
        throws Exception {
        BaseContextHandler.remove();
        super.afterCompletion(request, response, handler, ex);
    }
}
